import { store } from "@/store";
import { usePermissionStoreHook } from "@/store/modules/permission";
import { useDictStoreHook } from "@/store/modules/dict";

import AuthAPI, { type LoginFormData } from "@/api/auth";
import UserAPI, { type UserInfo } from "@/api/system/user";

//import { setAccessToken, setRefreshToken, getRefreshToken, clearToken } from "@/utils/auth";
import { setAccessToken, clearToken } from "@/utils/auth";
import { Menus } from "@/api/system/menu";

export const useUserStore = defineStore("user", () => {
  const userInfo = useStorage<UserInfo>("userInfo", {} as UserInfo);

  /**
   * 登录
   *
   * @param {LoginFormData}
   * @returns
   */
  function login(LoginFormData: LoginFormData) {
    return new Promise<void>((resolve, reject) => {
      AuthAPI.login(LoginFormData)
        .then(async (data) => {
          const { token, info, menus } = data;
          const { m, p } = menus.reduce(
            (result, item) => {
              if (item.isMenu) {
                result.m.push(item);
              } else {
                result.p.push(item);
              }
              return result;
            },
            { m: [] as Menus[], p: [] as Menus[] }
          );
          userInfo.value = {
            ...info, // 用户信息
            roles: [], // 确保数组存在
            perms: p as Menus[], // 菜单
            menus: m as Menus[], // 权限
          };

          setAccessToken(token);
          //console.log(menus);
          // eyJhbGciOiJIUzI1NiJ9.xxx.xxx
          // 后台管理不无线刷新，登陆失效后重新登陆，并且不能保存密码
          //setRefreshToken(refreshToken);
          resolve();
        })
        .catch((error) => {
          console.log(error);
          reject(error);
        });
    });
  }

  /**
   * 获取用户信息
   *
   * @returns {UserInfo} 用户信息
   */
  function getUserInfo() {
    return new Promise<UserInfo>((resolve, reject) => {
      UserAPI.getInfo()
        .then((data) => {
          if (!data) {
            reject("Verification failed, please Login again.");
            return;
          }
          Object.assign(userInfo.value, { ...data });
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 登出
   */
  function logout() {
    return new Promise<void>((resolve, reject) => {
      AuthAPI.logout()
        .then(() => {
          clearUserData();
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 刷新 token
   */
  // function refreshToken() {
  //   const refreshToken = getRefreshToken();
  //   return new Promise<void>((resolve, reject) => {
  //     AuthAPI.refreshToken(refreshToken)
  //       .then((data) => {
  //         const { accessToken, refreshToken } = data;
  //         setAccessToken(accessToken);
  //         setRefreshToken(refreshToken);
  //         resolve();
  //       })
  //       .catch((error) => {
  //         console.log(" refreshToken  刷新失败", error);
  //         reject(error);
  //       });
  //   });
  // }

  /**
   * 清理用户数据
   *
   * @returns
   */
  function clearUserData() {
    return new Promise<void>((resolve) => {
      clearToken();
      usePermissionStoreHook().resetRouter();
      useDictStoreHook().clearDictionaryCache();
      resolve();
    });
  }

  return {
    userInfo,
    getUserInfo,
    login,
    logout,
    clearUserData,
    //refreshToken,
  };
});

/**
 * 用于在组件外部（如在Pinia Store 中）使用 Pinia 提供的 store 实例。
 * 官方文档解释了如何在组件外部使用 Pinia Store：
 * https://pinia.vuejs.org/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
 */
export function useUserStoreHook() {
  return useUserStore(store);
}
